//
// Created by ade on 2022/11/10.
//
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    // 思考过程：
    // a    b     carry   a计算后  b计算后
    // 0101 111   1010    0010    1010
    // 0010 1010  0100    1000    0100
    // 1000 0100  0000    1100    0000
    int getSum1(int a, int b) {
        while (b != 0) {
            // unsigned因为防止溢出
            unsigned int carry = (unsigned int) (a & b) << 1; // a & b的结果左移一位即可得到进位 因为是进位，所以必须左移一位
            a = a ^ b;
            cout << "a:" << a << ",carry:" << carry << endl;
            b = carry;
        }
        return a;
    }

    int getSum(int a, int b) {
        while (b != 0) {
            unsigned int carry = (unsigned int) (a & b) << 1;
            a = a ^ b;
            b = carry;
        }
        return a;
    }
};

int main() {
    Solution so;
    cout << so.getSum(5, 7) << endl;// 101 111  1010
    return 0;
}
